package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.MenberMapper;
import com.hqyj.pojo.Menber;
import com.hqyj.service.MenberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


@Service
public class MenberServiceImpl implements MenberService {

    //注入mapper
    @Autowired(required = false)
    MenberMapper menberMapper;

    @Override
    public HashMap<String, Object> select(Menber m) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<Menber> page = new Page<>(m.getPage(),m.getRow());

        //条件构造器
        QueryWrapper<Menber> queryWrapper = new QueryWrapper<>();

//        queryWrapper.eq(m.getMenberId()!=null,"menber_id",m.getMenberId());
        queryWrapper.like(m.getMenberName()!=null&&!m.getMenberName().equals(""),"menber_name",m.getMenberName());

        //查询数据库
        Page<Menber> mList =menberMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",mList.getRecords());
        //总页数
        map.put("totalPage",mList.getPages());
        //总条数
        map.put("total",mList.getTotal());
        //当前页
        map.put("curPage",m.getPage());
        if(m.getPage()==1){
            //上一页
            map.put("prePage",1);
        }else{
            map.put("prePage",m.getPage()-1);
        }
        if(m.getPage()==mList.getPages()){
            //下一页
            map.put("nextPage",mList.getPages());
        }else{
            map.put("nextPage",m.getPage()+1);
        }
        //每页显示条数
        map.put("row",m.getRow());
        return map;
    }

    @Override
    public HashMap<String, Object> add(Menber m) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = menberMapper.insert(m);
        if(num>0){
                map.put("info","保存成功");
        }else{
                map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Menber m) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //修改
        int num = menberMapper.updateById(m);
        if(num>0){
                map.put("info","保存成功");
        }else{
                map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer m) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num = menberMapper.deleteById(m);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除错误");
        }
        return map;
    }
}
